package com.atguigu.gmall.activity.controller;

import com.atguigu.gmall.activity.service.SeckillBizService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.order.model.OrderInfo;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author: atguigu
 * @create: 2023-09-23 10:22
 */
@RestController
@RequestMapping("/api/activity/seckill")
public class SeckillBizController {

    @Autowired
    private SeckillBizService seckillBizService;

    /**
     * 为用户秒杀指定商品生成抢购码
     *
     * @param skuId
     * @return
     */
    @ApiOperation("为用户秒杀指定商品生成抢购码")
    @GetMapping("/auth/getSeckillSkuIdStr/{skuId}")
    public Result getSeckillSkuIdStr(HttpServletRequest request, @PathVariable("skuId") Long skuId) {
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isBlank(userId)) {
            throw new RuntimeException("请先登录");
        }
        String buyCode = seckillBizService.getSeckillSkuIdStr(userId, skuId);
        return Result.ok(buyCode);
    }

    /**
     * 秒杀入队，将用户秒杀请求放入MQ队列中
     *
     * @param request
     * @param skuId
     * @param buyCode
     * @return
     */
    @ApiOperation("秒杀请求入队")
    @PostMapping("/auth/seckillOrder/{skuId}")
    public Result seckillRequest2Queue(HttpServletRequest request, @PathVariable("skuId") Long skuId, @RequestParam(value = "skuIdStr", required = false) String buyCode) {
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isBlank(userId)) {
            throw new RuntimeException("请先登录");
        }
        seckillBizService.seckillRequest2Queue(userId, skuId, buyCode);
        return Result.ok();
    }


    /**
     * 处理前端定时查询秒杀结果
     *
     * @param skuId
     * @return
     */
    @ApiOperation("检查用户秒杀结果")
    @GetMapping("/auth/checkOrder/{skuId}")
    public Result checkSeckillStatus(HttpServletRequest request, @PathVariable("skuId") Long skuId) {
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isBlank(userId)) {
            throw new RuntimeException("请先登录");
        }
        return seckillBizService.checkSeckillStatus(userId, skuId);
    }

    /**
     * 汇总秒杀确认页面相关数据
     *
     * @param request
     * @param skuId
     * @return
     */
    @ApiOperation("汇总秒杀确认页面相关数据")
    @GetMapping("/auth/trade")
    public Result seckillTradeData(HttpServletRequest request, @RequestParam("skuId") Long skuId) {
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isBlank(userId)) {
            throw new RuntimeException("请先登录");
        }
        Map<String, Object> mapResult = seckillBizService.seckillTradeData(userId, skuId);
        return Result.ok(mapResult);
    }


    /**
     * 提交秒杀订单
     *
     * @param orderInfo
     * @return
     */
    @ApiOperation("提交秒杀订单")
    @PostMapping("/auth/submitOrder")
    public Result submitSeckillOrder(HttpServletRequest request, @RequestBody OrderInfo orderInfo) {
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isBlank(userId)) {
            throw new RuntimeException("请先登录");
        }
        orderInfo.setUserId(Long.valueOf(userId));
        Long orderId = seckillBizService.submitSeckillOrder(orderInfo);
        return Result.ok(orderId);
    }

}
